package blockworld;

import java.util.Comparator;

import grid.items.Body;



class BestTargetCompEBody implements Comparator<EBody>
{
	EBody _shooter = null;
	 
	public BestTargetCompEBody(EBody shooter)
	{
		this._shooter = shooter;
	}
	
	@Override
    public int compare(EBody body1, EBody body2)
    {
    	
    	//There are two things to compare too, the expected HP after shooting, and the expected damage.
    	if( body1.getExpectedHPAfterShoot(_shooter.getType()) < body2.getExpectedHPAfterShoot(_shooter.getType()) )
			return -1;
		else if( body1.getExpectedHPAfterShoot(_shooter.getType()) > body2.getExpectedHPAfterShoot(_shooter.getType()) )
			return 1;
		else  // same expectedHPAfterShoot
		{
			if( body1.getExpectedDamage(_shooter.getType()) > body2.getExpectedDamage(_shooter.getType()) )
				return -1;
			else if(body1.getExpectedDamage(_shooter.getType()) < body2.getExpectedDamage(_shooter.getType()))
				return 1;
			else
				return 0;
		}
    }

}




//Expected Body.
public class EBody 
{
	private String _name;
	private String _team;
	private int _type;
	private int _hp;
	
	public static final int STARTING_HP = 10;
	public static final int ROCK = 0;
	public static final int PAPER = 1;
	public static final int SCISSOR = 2;
	
	
	public EBody(Body body)
	{
		this(body, STARTING_HP);
	}
	
	public EBody(Body body, int hp)
	{
		this._name = body.getName();
		this._team = body.getTeam();
		this._type = body.getType();
		this._hp = hp;
	}
	
	public EBody(String name,String team, int type)
	{
		this(name,team,type,STARTING_HP);
	}
	
	public EBody(String name,String team, int type, int expectedHp)
	{
		this._name = name;
		this._team = team;
		this._type = type;
		this._hp = expectedHp;
	}
	
	public int getType() 
	{
		return _type;
	}

	public String getName() 
	{
		return _name;
	}
	
	public String getTeam()
	{
		return _team;
	}
	
	public int getExpectedHP()
	{
		return _hp;
	}
	
	public int getExpectedHPAfterShoot(int shooterType)
	{
		int damage = 1;
		
		if(EBody.dominates(shooterType, getType()))
			damage = 3;
		
		int hp = this._hp;
		hp = hp-damage;
		
		if(hp<0)
			hp = 0;
			
		return hp;
		
	}
	
	public int getExpectedDamage(int shooterType)
	{
		int hp = _hp;
		int hpAfter = getExpectedHPAfterShoot(shooterType);
		return (hp - hpAfter);
	}
	
	
	public void setExpectedHP(int hp)
	{
		if(hp <0)
			this._hp = 0;
		else
			this._hp = hp;
	}
	
	public String getID()
	{
		return _team + _name;
	}
	
	
	public void inflictDamage(int shooterType)
	{
		int damage = 1;
		
		if(EBody.dominates(shooterType, getType()))
			damage = 3;
		
		this._hp = _hp-damage;
		
		if(this._hp < 0)
			this._hp = 0;
	}
	
	public boolean sameTeam(EBody b)
	{
		return getTeam().equals(b.getTeam());
	}
	
	public boolean dominates(EBody b) 
	{
		return dominates(b.getType());
	}
	
	public boolean dominates(int otherType)
	{
		return 
		(this.getType()==ROCK && otherType==SCISSOR)
		||
		(this.getType()==PAPER && otherType==ROCK)
		||
		(this.getType()==SCISSOR && otherType==PAPER);
	}
	
	public static boolean dominates(int type, int otherType)
	{
		return
			(type == ROCK && otherType == SCISSOR)
			||
			(type == PAPER && otherType == SCISSOR)
			||
			(type == SCISSOR && otherType == PAPER)	
			;
	}

	public String getTypeString()
	{
		return EBody.typeToString(getType());
	}
	
	public static String typeToString(int type)
	{
		if(type == ROCK)
			return "rock";
		if(type == PAPER)
			return "paper";
		if(type == SCISSOR)
			return "scissor";
		else
			return "undefined body type";
	}
	
	public static int typeFromString(String type)
	{
		type = type.toLowerCase();
		if(type.equals("rock"))
			return ROCK;
		else if(type.equals("paper"))
			return PAPER;
		else if(type.equals("scissor"))
			return SCISSOR;
		else
			return -1;
	}

	public String toShortString(String friendlyTeam)
	{
		String result = "";
		
		if(getType() == ROCK)
		{
			result = "r";
		}
		else if(getType() == PAPER)
		{
			result = "p";
		}
		else if(getType() == SCISSOR)
		{
			result = "s";
		}
		
		if(!getTeam().equals(friendlyTeam))
		{
			result = result.toUpperCase();
		}
		if(getExpectedHP() >= 10)
			result+=getExpectedHP();
		else
			result+=getExpectedHP() + " ";
		
		return result;
	}

	public boolean equals(Object other)
	{
		if ( other == null )
	    {
	      return false;
	    }
		else if ( other instanceof EBody )
	    {
			EBody otherEbody = (EBody) other;
			return this.getID().equals(otherEbody.getID());
	    }
		else return false;
	}

}